package com.kede.api.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.kede.api.order.service.IStoreService;
import com.kede.dictenum.BoolDictEnum;
import com.kede.domain.*;
import com.kede.ienum.store.UserStoreStatusEnum;
import com.kede.pojo.PagePO;
import com.kede.pojo.ResHttp;
import com.kede.service.*;
import com.kede.utils.CollectionUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/***
 ** @Author:Pr.li
 ** @Description:
 ** @Date: create in 2023/6/8 16:30
 ***/
@Service
public class StoreServiceImpl implements IStoreService {

    @Resource
    private StoreUserOrderService storeUserOrderService;

    @Resource
    private SuppliesStoreService suppliesStoreService;

    @Resource
    private UsersService usersService;


    @Resource
    private SuppliesService suppliesService;


    @Override
    public ResHttp getStoreSupplieOrder(PagePO page, Integer storeId) {
        List<OrderItem> orders = page.orders();
        if (CollectionUtil.Blank(orders)){
            orders.add(OrderItem.desc("create_time"));
        }
        LambdaQueryWrapper<StoreUserOrderDomain> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StoreUserOrderDomain::getStoreId,storeId);
        queryWrapper.eq(StoreUserOrderDomain::getStoreIsdel, BoolDictEnum.FALSE.getCode());
        PagePO<StoreUserOrderDomain> page1 = storeUserOrderService.page(page, queryWrapper);
        page1.getRecords().forEach(order -> {
            SuppliesDomain supplie = suppliesService.getById(order.getSupplieId());
            UsersDomain user = usersService.getById(order.getUserId());
            order.setUser(user);
            order.setSupplie(supplie);
        });
        return ResHttp.okData(page1);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResHttp SureExchangeSupplie(String imgarrStr, Integer orderId) {
        StoreUserOrderDomain storeUserOrderDomain = storeUserOrderService.getById(orderId);
        storeUserOrderDomain.setImgs(imgarrStr);
        storeUserOrderDomain.setUpdateTime(LocalDateTime.now());
        storeUserOrderDomain.setStatus(UserStoreStatusEnum.FINISH);
        storeUserOrderService.updateById(storeUserOrderDomain);
        return ResHttp.ok();
    }

    @Override
    public ResHttp delStoreSupplieOrder(Integer orderId) {
        StoreUserOrderDomain storeUserOrderDomain = storeUserOrderService.getById(orderId);
        storeUserOrderDomain.setStoreIsdel(BoolDictEnum.TRUE.getCode());
        storeUserOrderService.updateById(storeUserOrderDomain);
        return ResHttp.ok();
    }

}
